package com.skycode5.servlet;

import com.skycode5.bean.NewsBean;
import com.skycode5.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 1.接受post网址 /newsAdd.jsp
 * 2.接受收到传递过来的新闻参数 新闻标题 新闻内容
 * 3.完成新闻的添加
 * 4.跳转到其他页面
 */
@WebServlet("/newsAdd.jsq")
public class NewsAddservlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setCharacterEncoding("utf-8");
        String newsTitle = req.getParameter("newsTitle");
        String newsContent = req.getParameter("newsContent");
        NewsBean newsBean = new NewsBean();
        newsBean.setNewsTitle(newsTitle);
        newsBean.setNewsContent(newsContent);
        NewsDao newsDao = new NewsDao();
        newsDao.add(newsBean);
        try {
            req.getRequestDispatcher("new.jsp").forward(req,resp);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    }
}